package com.decompiler.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

/**
 * 现代化标题栏
 */
public class ModernTitleBar extends JPanel {
    private JFrame parentFrame;
    private String title;
    private Color backgroundColor;
    private Color textColor;
    private Font titleFont;
    private JButton closeButton;
    private JButton minimizeButton;
    private JButton maximizeButton;
    private Point initialClick;
    private boolean isDraggable;
    
    /**
     * 创建一个现代化标题栏
     * 
     * @param parentFrame 父窗口
     * @param title 标题
     * @param backgroundColor 背景颜色
     * @param textColor 文本颜色
     * @param isDraggable 是否可拖动
     */
    public ModernTitleBar(JFrame parentFrame, String title, Color backgroundColor, Color textColor, boolean isDraggable) {
        this.parentFrame = parentFrame;
        this.title = title;
        this.backgroundColor = backgroundColor;
        this.textColor = textColor;
        this.isDraggable = isDraggable;
        this.titleFont = new Font(Font.SANS_SERIF, Font.BOLD, 14);
        
        setupUI();
        if (isDraggable) {
            setupDraggable();
        }
    }
    
    /**
     * 创建一个使用默认颜色的现代化标题栏
     * 
     * @param parentFrame 父窗口
     * @param title 标题
     */
    public ModernTitleBar(JFrame parentFrame, String title) {
        this(parentFrame, title, new Color(59, 89, 152), Color.WHITE, true);
    }
    
    private void setupUI() {
        setLayout(new BorderLayout());
        setBackground(backgroundColor);
        setPreferredSize(new Dimension(getWidth(), 40)); // 设置标题栏高度
        
        // 创建标题标签
        JLabel titleLabel = new JLabel(title);
        titleLabel.setForeground(textColor);
        titleLabel.setFont(titleFont);
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 15, 0, 0));
        
        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
        buttonPanel.setOpaque(false);
        
        // 创建最小化按钮
        minimizeButton = createButton("_", new Color(70, 100, 170));
        minimizeButton.addActionListener(e -> parentFrame.setState(JFrame.ICONIFIED));
        
        // 创建最大化按钮
        maximizeButton = createButton("□", new Color(70, 100, 170));
        maximizeButton.addActionListener(e -> {
            if ((parentFrame.getExtendedState() & JFrame.MAXIMIZED_BOTH) == JFrame.MAXIMIZED_BOTH) {
                parentFrame.setExtendedState(JFrame.NORMAL);
            } else {
                parentFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            }
        });
        
        // 创建关闭按钮
        closeButton = createButton("×", new Color(232, 17, 35));
        closeButton.addActionListener(e -> {
            // 触发窗口关闭事件
            parentFrame.dispatchEvent(new java.awt.event.WindowEvent(
                    parentFrame, java.awt.event.WindowEvent.WINDOW_CLOSING));
        });
        
        // 添加按钮到面板
        buttonPanel.add(minimizeButton);
        buttonPanel.add(maximizeButton);
        buttonPanel.add(closeButton);
        
        // 添加组件到标题栏
        add(titleLabel, BorderLayout.WEST);
        add(buttonPanel, BorderLayout.EAST);
    }
    
    private JButton createButton(String text, Color hoverColor) {
        JButton button = new JButton(text) {
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                
                if (getModel().isPressed()) {
                    g2d.setColor(hoverColor.darker());
                } else if (getModel().isRollover()) {
                    g2d.setColor(hoverColor);
                } else {
                    g2d.setColor(getBackground());
                }
                
                g2d.fillRect(0, 0, getWidth(), getHeight());
                
                // 绘制文本
                FontMetrics fm = g2d.getFontMetrics();
                int x = (getWidth() - fm.stringWidth(getText())) / 2;
                int y = (getHeight() - fm.getHeight()) / 2 + fm.getAscent();
                
                g2d.setColor(getForeground());
                g2d.drawString(getText(), x, y);
                g2d.dispose();
            }
        };
        
        button.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
        button.setForeground(Color.WHITE);
        button.setBackground(backgroundColor);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setContentAreaFilled(false);
        button.setPreferredSize(new Dimension(45, 30));
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        
        return button;
    }
    
    private void setupDraggable() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                initialClick = e.getPoint();
            }
        });
        
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                // 获取当前位置
                int thisX = parentFrame.getLocation().x;
                int thisY = parentFrame.getLocation().y;
                
                // 计算移动距离
                int xMoved = e.getX() - initialClick.x;
                int yMoved = e.getY() - initialClick.y;
                
                // 移动窗口
                int X = thisX + xMoved;
                int Y = thisY + yMoved;
                parentFrame.setLocation(X, Y);
            }
        });
    }
    
    /**
     * 设置标题
     * 
     * @param title 标题文本
     */
    public void setTitleText(String title) {
        this.title = title;
        for (Component comp : getComponents()) {
            if (comp instanceof JLabel) {
                ((JLabel) comp).setText(title);
                break;
            }
        }
    }
}